﻿using System;
using System.Windows.Input;

namespace Socium.Presentation {
	public abstract class LineItemBase<T> : NamedViewModel {
		protected LineItemBase(T model) {
			_model = model;

			InitializeCommands();

			Update();
		}

		void InitializeCommands() {
			EditCommand = new Command(OnEdit);
			DeleteCommand = new Command(OnDelete);
		}

		#region Model

		readonly T _model;

		public T Model {
			get { return _model; }
		}

		#endregion

		#region GetDisplayName

		protected abstract string GetDisplayName(T model);

		#endregion

		#region Update

		public void Update() {
			DisplayName = GetDisplayName(Model);
		}

		#endregion

		#region Edit

		public ICommand EditCommand { get; private set; }

		public event Action<T> QueryEdit;

		void OnEdit() {
			var handler = QueryEdit;
			if (handler != null) {
				handler(Model);
			}
		}

		#endregion

		#region Delete

		public ICommand DeleteCommand { get; private set; }

		
		public event Action<T> QueryDelete;

		void OnDelete() {
			var handler = QueryDelete;
			if (handler != null) {
				handler(Model);
			}
		}

		#endregion
	}
}